﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DingY.DB;

namespace DotNet.WinUI.Customer
{
    public partial class FrmCustomerEdit : BaseForm
    {
        public FrmCustomerEdit()
        {
            InitializeComponent();
        }

        private string CustomerID = "0";
        private string Operate = "New";

        public FrmCustomerEdit(string operate, string customerID)
        {
            InitializeComponent();

            CustomerID = customerID;
            Operate = operate;
        }       

        private void SaveData()
        {
            try
            {
                if (txtName.Text.Length <= 0)
                {
                    ShowMsg("请输入姓名！");
                    return;
                }

                if (txtMobile.Text.Length <= 0)
                {
                    ShowMsg("请输入手机号码！");
                    return;
                }

                if (txtCompanyName.Text.Length <= 0)
                {
                    ShowMsg("请输入所属公司名称！");
                    return;
                }

                if (cmbDuties.SelectedIndex == 0)
                {
                    ShowMsg("请选择职务！");
                    return;
                }

                if (string.Equals(cmbIndustry.SelectedValue.ToString(), "0"))
                {
                    ShowMsg("请选择行业！");
                    return;
                }

                if(!ValidatePhone(txtMobile.Text))
                {
                    ShowMsg("手机号码格式不正确，请输入以13、14、15、18开始的11位号码！");
                    return;
                }

                string _Sql = null;
                SqlDB objSqlDB = new SqlDB();
                if (CustomerID == "0")
                {
                    _Sql = @"SELECT Count(Name) FROM Customer WHERE Name ='{0}' ";
                    _Sql = string.Format(_Sql, txtName.Text);

                    if (Convert.ToInt32(objSqlDB.ExecuteScalar(_Sql)) >= 1)
                    {
                        ShowMsg("名称已经存在，请重新输入！");
                        txtName.Focus();
                        return;
                    }

                    _Sql = @"INSERT INTO Customer(Name, Duties, IndustryID, Birthday, Mobile, QQ, Email, 
                            CompanyName, WebSite, Phone, Fax, Address, Depict) 
                        VALUES('{0}', {1}, {2}, '{3}', '{4}', '{5}', '{6}', 
                            '{7}', '{8}', '{9}', '{10}', '{11}', '{12}') ";
                }
                else
                {
                    _Sql = @"UPDATE Customer
                        SET Name = '{0}', Duties = {1}, IndustryID = {2}, Birthday = '{3}', 
                            Mobile = '{4}', QQ = '{5}', Email = '{6}', CompanyName = '{7}', WebSite = '{8}', 
                            Phone = '{9}', Fax = '{10}', Address= '{11}', Depict= '{12}'
                        WHERE CustomerID = '{13}' ";
                }
                _Sql = string.Format(_Sql,
                    txtName.Text,
                    cmbDuties.SelectedIndex,
                    cmbIndustry.SelectedValue,
                    txtBirthday.Value.ToString("yyyy-MM-dd"),
                    txtMobile.Text,
                    txtQQ.Text,
                    txtEmail.Text,
                    txtCompanyName.Text,
                    txtWebSite.Text,
                    txtPhone.Text,
                    txtFax.Text,
                    txtAddress.Text,
                    txtDepict.Text,
                    CustomerID);

                if (objSqlDB.ExecuteSql(_Sql) >= 1)
                {
                    ShowMsg("数据保存成功!");
                }
            }
            catch (Exception ex)
            {
                ShowMsg(ex.Message);
            }
        }

        private void LoadCustomer(string CustomerID)
        {
            string sql = @"SELECT *
                FROM Customer
                WHERE CustomerID = '{0}' ";
            sql = string.Format(sql, CustomerID);
            SqlDB objSqlDB = new SqlDB();
            DataSet ds = objSqlDB.ExecuteDataSet(sql);
            DataRow row = ds.Tables[0].Rows[0];

            txtBirthday.Text = row["Birthday"].ToString();
            txtEmail.Text = row["Email"].ToString();
            txtMobile.Text = row["Mobile"].ToString();
            txtName.Text = row["Name"].ToString();
            txtQQ.Text = row["QQ"].ToString();
            txtWebSite.Text = row["WebSite"].ToString();
            txtCompanyName.Text = row["CompanyName"].ToString();
            txtPhone.Text = row["Phone"].ToString();
            cmbIndustry.SelectedValue = row["IndustryID"].ToString();
            cmbDuties.SelectedIndex = int.Parse(row["Duties"].ToString());
            txtFax.Text = row["Fax"].ToString();
            txtAddress.Text = row["Address"].ToString();
            txtDepict.Text = row["Depict"].ToString();

        }

        private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            switch (e.ClickedItem.Text)
            {
                case "保存":
                    SaveData();
                    break;
                case "关闭":
                    Close();
                    break;
            }
        }

        private void FrmCustomerEdit_Load(object sender, EventArgs e)
        {
            FillIndustry(cmbIndustry, "请选择数据...");

            cmbDuties.SelectedIndex = 0;

            txtBirthday.Text = DateTime.Now.ToShortDateString();

            if (CustomerID != "0")
            {
                LoadCustomer(CustomerID);
            }

            if(Operate == "Find")
            {
                btnSave.Visible = false;
            }
        }
    }
}
